package cuongnh.project.japanesediary.vocabulary.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "tbl_kanji")
public class Kanji extends AbstractModelObject {

	@Id
	@Column(name = "kanji_id")
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="kanji_seq")
	@SequenceGenerator(
			name="kanji_seq",
			sequenceName="kanji_sequence",
			allocationSize=1
		)
	private Long kanjiId;

	@Column(name = "kanji", length = 1)
	private String kanji;

	@Column(name = "onyomi", length = 20)
	private String onyomi;

	@Column(name = "kunyomi", length = 20)
	private String kunyomi;
	
	@Column(name = "hanviet", length = 20)
	private String hanviet;
	
	@Column(name = "strokes")
	private Short strokes;
	
	@Column(name = "grade")
	private Short grade;
	
	@Column(name = "frequency")
	private Integer frequency;

	@Column(name = "meaning_vietnamese", length = 50)
	private String vnMeaning;

	@Column(name = "meaning_english", length = 50)
	private String enMeaning;

	@Column(name = "meaning_japanese", length = 50)
	private String jpMeaning;

	@Column(name = "explaination_vietnamese", length = 1000)
	private String vnExplaination;

	@Column(name = "explaination_english", length = 1000)
	private String enExplaination;

	@Column(name = "explaination_japanese", length = 1000)
	private String jpExplaination;
	
	@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_kanji_radical", joinColumns = { @JoinColumn(name = "kanji_id") }, inverseJoinColumns = { @JoinColumn(name = "radical_id") })
	private Set<Radical> radicals = new HashSet<Radical>();

	@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_kanji_compound_word", joinColumns = { @JoinColumn(name = "kanji_id") }, inverseJoinColumns = { @JoinColumn(name = "vocabulary_id") })
	private Set<Vocabulary> compoundWords = new HashSet<Vocabulary>();
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_kanji_category", joinColumns = { @JoinColumn(name = "kanji_id") }, inverseJoinColumns = { @JoinColumn(name = "category_id") })
	private Set<Category> kanjiCategories = new HashSet<Category>();
	
	@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_text_kanji", joinColumns = { @JoinColumn(name = "kanji_id") }, inverseJoinColumns = { @JoinColumn(name = "text_id") })
	private Set<Text> kanjiTexts = new HashSet<Text>();
	
	@Column(name = "created_date")
	private Date createdDate;

	@Column(name = "modified_date")
	private Date modifiedDate;
	
	public long getKanjiId() {
		return kanjiId;
	}

	public void setKanjiId(long kanjiId) {
		this.kanjiId = kanjiId;
	}

	public String getKanji() {
		return kanji;
	}

	public void setKanji(String kanji) {
		this.kanji = kanji;
	}

	public String getOnyomi() {
		return onyomi;
	}

	public void setOnyomi(String onyomi) {
		this.onyomi = onyomi;
	}

	public String getKunyomi() {
		return kunyomi;
	}

	public void setKunyomi(String kunyomi) {
		this.kunyomi = kunyomi;
	}

	public String getHanviet() {
		return hanviet;
	}

	public void setHanviet(String hanviet) {
		this.hanviet = hanviet;
	}

	public Short getStrokes() {
		return strokes;
	}

	public void setStrokes(Short strokes) {
		this.strokes = strokes;
	}

	public Short getGrade() {
		return grade;
	}

	public void setGrade(Short grade) {
		this.grade = grade;
	}

	public Integer getFrequency() {
		return frequency;
	}

	public void setFrequency(Integer frequency) {
		this.frequency = frequency;
	}

	public String getVnMeaning() {
		return vnMeaning;
	}

	public void setVnMeaning(String vnMeaning) {
		this.vnMeaning = vnMeaning;
	}

	public String getEnMeaning() {
		return enMeaning;
	}

	public void setEnMeaning(String enMeaning) {
		this.enMeaning = enMeaning;
	}

	public String getJpMeaning() {
		return jpMeaning;
	}

	public void setJpMeaning(String jpMeaning) {
		this.jpMeaning = jpMeaning;
	}

	public String getVnExplaination() {
		return vnExplaination;
	}

	public void setVnExplaination(String vnExplaination) {
		this.vnExplaination = vnExplaination;
	}

	public String getEnExplaination() {
		return enExplaination;
	}

	public void setEnExplaination(String enExplaination) {
		this.enExplaination = enExplaination;
	}

	public String getJpExplaination() {
		return jpExplaination;
	}

	public void setJpExplaination(String jpExplaination) {
		this.jpExplaination = jpExplaination;
	}

	public Set<Radical> getRadicals() {
		return radicals;
	}

	public void setRadicals(Set<Radical> radicals) {
		this.radicals = radicals;
	}

	public Set<Vocabulary> getCompoundWords() {
		return compoundWords;
	}

	public void setCompoundWords(Set<Vocabulary> compoundWords) {
		this.compoundWords = compoundWords;
	}
	
	public Set<Category> getKanjiCategories() {
		return kanjiCategories;
	}

	public void setKanjiCategories(Set<Category> kanjiCategories) {
		Set<Category> oldValue = this.kanjiCategories;
		this.kanjiCategories = kanjiCategories;
		firePropertyChange("kanjiCategories", oldValue, kanjiCategories);
	}
	
	public Set<Text> getKanjiTexts() {
		return kanjiTexts;
	}

	public void setKanjiTexts(Set<Text> kanjiTexts) {
		Set<Text> oldValue = this.kanjiTexts;
		this.kanjiTexts = kanjiTexts;
		firePropertyChange("kanjiTexts", oldValue,
				kanjiTexts);
	}

	public void setKanjiId(Long kanjiId) {
		this.kanjiId = kanjiId;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public Date getModifiedDate() {
		return modifiedDate;
	}

	public void setModifiedDate(Date modifiedDate) {
		this.modifiedDate = modifiedDate;
	}

	@Override
	public boolean equals(Object obj) {
		boolean sameSame = false;

		if (obj != null && obj instanceof Kanji) {
			sameSame = kanjiId.equals(((Kanji) obj).kanjiId);
		}

		return sameSame;
	}
	
	@Override
	public int hashCode() {
		return this.kanjiId.intValue();
	}

}
